<?php


class article
{

    public function __construct()
    {
        require_once("pdo.php");
    }


    /**
     * 获取文章插入数据库
     *
     * @param  str $url       [description]
     *
     * @param  str $tableName [description]
     *
     */
    public function getArticle($url,$tableName)
    {
        $data = $this->_getUrlInfo($url);

        $db = pdoMysql::getInstance();
        $reg = $db->addConfig('reg');
        if(!($this->_regArticle($reg,$data))){
            exit("正则有误：$reg");
        }
        $article = $this->_regArticle($reg,$data);

        $db->insert($article,$tableName);
    }


    /**
     * 匹配文章
     *
     * @param  xml $data [description]
     *
     * @return arr
     */
    private function _regArticle($reg,$data)
    {
        $article = '';
        foreach ($reg as $k => $v) {

            preg_match($v, $data, $oldArticle);
            $article[$k] = $oldArticle[1];
       }

        return $article;
    }

     /**
     * 抓取网页源 数据
     *
     * @param  str $url [description]
     *
     * @return xml
     */
    private function _getUrlInfo ($url)
    {

        $ch = curl_init();
        $timeout = 10;

        curl_setopt ($ch, CURLOPT_URL, $url);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

        $urlInfo = curl_exec($ch);
        curl_close($ch);

        return $urlInfo;
    }

}
